import {SAVE_CANVAS_LINE_ARR, SAVE_ENDOGENOUS_CONTROL_TARGET, SAVE_GROUP, SAVE_REFERENCE_SAMPLE, SAVE_STANDARD_OBJ, SAVE_TARGET_NAME, SAVE_SAMPLES_, SAVE_REFERENCE_GROUP} from "./constants";
import storageSession from "redux-persist/lib/storage/session";
import {persistReducer} from "redux-persist";
import {generateUUID} from "../../../../common/utils";
import {WELL_TYPE} from "../../store/constants";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: [""] //  白名单:runStart会存入缓存，其他不会存
};

const defaultState = {
  reduxGroup: [{
    uuid: generateUUID(),
    opt: false,
    name: "",
    color: "#e70c24",
    comments: ""
  }],
  reduxTargetNameArr: [],
  analysisShowLineArr: [],
  standardObj: [],
  referenceSample: "",
  referenceGroup: "",
  endogenousControlTarget: [],
  samples_: {
    sampleRange: "Select",
    namePrefix: 2022,
    startingValue: 1,
    interval: 1,
    digits: 3,
    isDown: true,
    task: WELL_TYPE.UNK,
    fileName: "PCR_2.0"
  }
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case SAVE_GROUP :
      return {...state, reduxGroup: action.reduxGroup};
    case SAVE_TARGET_NAME :
      return {...state, reduxTargetNameArr: action.reduxTargetNameArr};
    case SAVE_CANVAS_LINE_ARR :
      return {...state, analysisShowLineArr: action.analysisShowLineArr};
    case SAVE_STANDARD_OBJ :
      return {...state, standardObj: action.standardObj};
    case SAVE_REFERENCE_SAMPLE :
      return {...state, referenceSample: action.referenceSample};
    case SAVE_REFERENCE_GROUP :
      return {...state, referenceGroup: action.referenceGroup};
    case SAVE_ENDOGENOUS_CONTROL_TARGET :
      return {...state, endogenousControlTarget: action.endogenousControlTarget};
    case SAVE_SAMPLES_ :
      return {...state, samples_: action.samples_};
    default:
      return state;
  }
}

export default persistReducer(persistConfig, reducer);
